<template>
  <div class="printForm">
    <div class="area-header">
      <span class="area-header-title">{{ $t('申请单') }}</span>
    </div>
    <table class="printTable">
      <tr>
        <td class="tit">{{ $t('申请标题') }}</td>
        <td colspan="3">{{ form.actTitle }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('合伙企业名称') }}</td>
        <td>{{ form.name }}</td>

        <td class="tit">{{ $t('执行事务合伙人') }}</td>
        <td>{{ form.executivePartner }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('执行事务合伙人委派代表') }}</td>
        <td>
          {{ form.appoints }}
        </td>
        <td class="tit">{{ $t('成立时间') }}</td>
        <td>
          {{ parseTime(form.established) }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('统一社会信用代码') }}</td>
        <td>
          {{ form.creditCode }}
        </td>
        <td class="tit">{{ $t('执照登记日期') }}</td>
        <td>
          {{ form.registerDate }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('合伙期限') }}</td>
        <td>
          {{ form.partnershipTerm }}
        </td>
        <td class="tit">{{ $t('基本户') }}</td>
        <td>
          {{ form.baseAccount }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('经营场所') }}</td>
        <td>
          {{ form.businessPlace }}
        </td>
        <td class="tit">{{ $t('经营范围') }}</td>
        <td>
          {{ form.businessRange }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('基金管理人') }}</td>
        <td>
          {{ form.gpName }}
        </td>
        <td class="tit">{{ $t('是否备案') }}</td>
        <td>
          {{ form.recordName }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('备注') }}</td>
        <td colspan="3">
          {{ form.remark }}
        </td>
      </tr>
    </table>
    <div v-if="form.hhqyEquityStructures.length > 0">
      <div class="area-header">
        <span class="area-header-title">{{ $t('股东信息') }}</span>
      </div>
      <!-- <el-table :data="form.hhqyEquityStructures" border>
        <el-table-column :label="$t('股东名称')" align="center" prop="name" />
        <el-table-column :label="$t('持股比例(%)')" align="center" prop="shareholdingRatio" />
        <el-table-column :label="$t('投资金额') + $t('金额单位')" align="center" prop="investAmount">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.investAmount, 'w') }}</span>
          </template>
        </el-table-column>
      </el-table> -->
      <table class="previewTable">
        <thead>
          <th>{{ $t('股东名称') }}</th>
          <th>{{ $t('持股比例(%)') }}</th>
          <th>{{ $t('投资金额') + $t('金额单位') }}</th>
        </thead>
        <tbody>
          <tr v-for="(v, ind) in form.hhqyEquityStructures" :key="ind">
            <td>{{ v.name }}</td>
            <td>{{ v.shareholdingRatio }}</td>
            <td>{{ moneyFormat(v.investAmount,'w') }}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <FileView :list.sync="form.fileList" data-type="fund_matters" />
  </div>
</template>

<script>
import {
  getHhqyBase,
  getHhqyDetail,
  listHhqyBase
} from '@/api/hhqy/base/hhqyBase'
import { companyListByName } from '@/api/company/base/companyBase'
import { Notification } from 'element-ui'
export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 股东名称列表数据
      restaurants: [],

      // 天眼查查询条件
      tycParam: {
        id: '',
        companyName: '',
        tycId: ''
      },
      btnHide: false
    }
  },
  created() {},
  mounted() {
    this.$bus.$emit('async_done')
    this.btnHide = this.$refs.formBtn.btnHide
    listHhqyBase().then(response => {
      this.restaurants = response.rows.map(ele => {
        return { value: ele.name, id: ele.id }
      })
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getHhqyBase(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        executivePartner: undefined,
        appoints: undefined,
        established: undefined,
        creditCode: undefined,
        registerDate: undefined,
        partnershipTerm: undefined,
        baseAccount: undefined,
        businessPlace: undefined,
        businessRange: undefined,
        delFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        remark: undefined,
        tycId: undefined,
        hhqyEquityStructures: []
      }
      this.resetForm('form')
    },

    // 标的公司查询
    companyQuerySearch(queryString, cb) {
      companyListByName(queryString).then(res => {
        if (res.code === 200) {
          cb(res.data)
        }
      })
    },
    // 标的公司选择后事件
    companyHandleSelect(item) {
      this.loading = true
      var name = this.form.name
      if (name === '' || name === undefined) {
        Notification.error({
          title: '请先输入公司全称！！'
        })
        this.loading = false
        return
      }
      this.resetForm()
      this.tycParam.id = this.form.id
      this.tycParam.tycId = item.id
      this.tycParam.name = name
      getHhqyDetail(this.tycParam).then(res => {
        if (res.code === 200) {
          if (res.msg === 'exist') {
            this.form.mediationName = undefined
            Notification.error({
              title: this.$t('合伙企业全称已存在！！')
            })
            this.loading = false
          } else if (res.msg === 'nofind') {
            this.form.companyName = undefined
            Notification.error({
              title: this.$t('天眼查没有获取到该合伙企业信息！！')
            })
            this.loading = false
          } else {
            // 公司简称
            this.form.name = name
            // 企业统一社会信用代码
            this.form.creditCode = res.data.creditCode
            // 成立时间
            this.form.established = res.data.established
            // 天眼查ID
            this.form.tycId = res.data.tycId
            this.msgSuccess(this.$t('已获取工商数据！'))
            this.loading = false
          }
        }
      })
    },
    // 初始化天眼查获取的数据
    resetForm() {
      this.form.name = undefined
      this.form.creditCode = undefined
      this.form.estiblishTime = undefined
      this.form.tycId = undefined
    },
    querySearch(queryString, cb, index) {
      var restaurants = this.restaurants
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      if (results.length === 0) {
        this.form.hhqyEquityStructures[index].shareholderId = undefined
      }
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return restaurant => {
        return (restaurant.value.toLowerCase().includes(queryString.toLowerCase()))
      }
    },
    shareholderOnSelect(val, index) {
      this.form.hhqyEquityStructures[index].shareholderId = val.id
    },
    addShareholder() {
      const row = {
        id: undefined,
        name: '',
        shareholdingRatio: undefined,
        investAmount: undefined
      }
      if (!(this.form.hhqyEquityStructures instanceof Array)) {
        this.$set(this.form, 'hhqyEquityStructures', [])
      }
      this.$nextTick(() => {
        this.form.hhqyEquityStructures.push(row)
      })
    },
    delShareholder(index) {
      this.form.hhqyEquityStructures.splice(index, 1)
    }
  }
}
</script>
